package com.aier.admin_new.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.admin_new.entity.RoleMenus;


public class RoleMenusRowMapper implements RowMapper<RoleMenus>{

	
	
	public RoleMenusRowMapper() {
		super();
	}

	@Override
	public RoleMenus mapRow(ResultSet rs, int rowNum) throws SQLException {
		return parseRoleMenus( new RoleMenus() ,rs);
	}

	private RoleMenus parseRoleMenus(RoleMenus roleMenus, ResultSet rs) {
		return parseUser(roleMenus, rs, null);
	}

	private RoleMenus parseUser(RoleMenus u, ResultSet rs, String su) {
		
		if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
		try {
            u.setMid(rs.getInt(su + "mid"));
        } catch (SQLException e) {
            u.setMenuname(null);
        }
		
		try {
            u.setMenuname(rs.getString(su + "menuname"));
        } catch (SQLException e) {
            u.setMenuname(null);
        }
        try {
            u.setMenupath(rs.getString(su + "menupath"));
        } catch (SQLException e) {
            u.setMenupath(null);
        }
        try {
            u.setRoletype(rs.getInt(su + "roletype"));
        } catch (SQLException e) {
            u.setRoletype(0);
        }
        try {
            u.setMenuid(rs.getString(su + "menuid"));
        } catch (SQLException e) {
            u.setRoletype(0);
        }
        
        try {
            u.setParentid(rs.getString(su + "parentid"));
        } catch (SQLException e) {
            u.setRoletype(0);
        }
        
        try {
            u.setImage(rs.getString(su + "image"));
        } catch (SQLException e) {
            u.setImage("");
        }
		return u;
	}
	
	 public static Map<String, Object> getPar(Map<String, Object> par , RoleMenus roleMenus){
		 par.put("mid",   roleMenus.getMid());
		 par.put("menuname", roleMenus.getMenuname());
		 par.put("menupath", roleMenus.getMenupath());
		 par.put("roletype", roleMenus.getRoletype());
		 par.put("menuid", roleMenus.getMenuid());
		 par.put("parentid", roleMenus.getParentid());
		 par.put("image", roleMenus.getImage());
		 return par;
	 }
	 
	 public static Map<String, Object> getArgs(Map<String, Object> args , RoleMenus roleMenus){
		 args.put("mid",   roleMenus.getMid());
		 return args;
	 }
	 
	 /**
     * 插入方法
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final RoleMenus u) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(u),
                        new String[] { "mid" });
                parsePS(ps, u);
                return ps;
            }
        };
    }

	private static void parsePS(PreparedStatement ps, RoleMenus u) throws SQLException {
		 int count = 1;
	        if (null != u.getMenuname()) {
	            ps.setString(count++, u.getMenuname());
	        }
	        if (null != u.getMenupath()) {
	            ps.setString(count++, u.getMenupath());
	        }
	        if (null != u.getRoletype()) {
	            ps.setInt(count++, u.getRoletype());
	        }
	}
	private static String parseSQL(RoleMenus u) throws SQLException{
		  StringBuffer sql = new StringBuffer("insert into user(");
	        int count = 0;
	        if (null != u.getMenuname()) {
	            sql.append("menuname,");
	            count++;
	        }
	        
	        if (null != u.getMenupath()) {
	            sql.append("menupath,");
	            count++;
	        }
	        
	        if (null != u.getRoletype()) {
	            sql.append("roletype,");
	            count++;
	        }
	        
	        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
	        sql.append(") value(");
	        for (int i = 0; i < (count - 1); i++) {
	            sql.append("?,");
	        }
	        return sql.append("?)").toString();
	}
	
}